//
//  HRProgressBackView.m
//  HWProgress
//
//  Created by mac on 2017/5/27.
//  Copyright © 2017年 hero_wqb. All rights reserved.
//

#import "HRProgressBackView.h"
#define KHWCircleLineWidth 4.0f

@interface HRProgressBackView ()
@property (nonatomic, strong) NSTimer *timer;

@end
@implementation HRProgressBackView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        HRRecordCircleView *circleView = [HRRecordCircleView new];
        [self addSubview:circleView];
        self.circleView = circleView;
        self.backgroundColor = [UIColor clearColor];
        WeakSelf
        [circleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf);
        }];

    }
    return self;
}


-(void)drawRect:(CGRect)rect{
    UIBezierPath *path = [[UIBezierPath alloc] init];

    
    CGFloat radius = (MIN(self.frame.size.width, self.frame.size.height) - KHWCircleLineWidth) * 0.5;
    [[UIColor groupTableViewBackgroundColor] set];
    path.lineWidth = KHWCircleLineWidth;

    [path addArcWithCenter:(CGPoint){self.frame.size.width * 0.5, self.frame.size.height * 0.5} radius:radius startAngle:M_PI * 1.5 endAngle:M_PI * 1.5 + M_PI * 2 clockwise:YES];
    
    [path stroke];

}

- (void)addTimer
{
    self.circleView.timeLabel.text = @"0S";
    self.circleView.progress = 0;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)timerAction
{
    _circleView.progress += 0.0016;
    
    if (_circleView.progress >= 1) {
        [self removeTimer];
    }
}

- (void)removeTimer
{
    [_timer invalidate];
    _timer = nil;
}


- (void)againRecord{
    self.circleView.progress = 0;
}

@end
